#include "myUart.h"
#include <string.h>
#include "myDevice.h"
#include  "crc.h"
/////////////////////////////////     串口接收发送     ///////////////////////////////////////////////////////////
void process_PCdata(u8 *buf, u16 id, u8 datalen)
{
	switch (id)
	{
	case 0x0000:
		break;
	case 0x0001:
		break;
	case 0x4001://心跳参数设置
		Heartbeat_interval = (buf[0] << 8) | buf[1];
		break;
	case 0x4200://驱动反馈
		break;
	case 0x4004://单路输出控制
		outputControl(buf[0], buf[1]);
		break;
	case 0x4005://多路输出控制
		outputsControl(buf); 
		break;
	case 0x4006://多组输出控制
		outputGroupControl(buf);
		break;
	case 0x4007://灯光
		led_state = buf[0];
		break;
	case 0x9001://手柄
		uart_send(&huart1, 0x9001, buf, datalen);
		break;
	}
}

uint8_t rx_buffer1[255];
uint16_t buffsize1 = sizeof(rx_buffer1);
uint16_t rx_len1 = 0;
volatile uint8_t uart1recv_end_flag = 0;

uint8_t rx_buffer3[255];
uint8_t buffsize3 = sizeof(rx_buffer3);
uint8_t rx_len3 = 0;
volatile uint8_t uart3recv_end_flag = 0;

uint8_t buf_toPC[6] = {0x48, 0x44, 0x00, 0x00, 0x42, 0x09};//HD(0x48,0x44)handle手柄
u8 tempdata[100]={0};// 转义字符缓存
uint8_t cmdData[100];

u8 buf_FrZboard_head[2] = {0x49,0x46};

void uart_receive(void)
{
	/* 串口1 */
	if (uart1recv_end_flag == 1)
	{
//		HAL_UART_Transmit(&huart1,rx_buffer1,rx_len1,0xff);
    uartData_depart(rx_buffer1, rx_len1);
		for (uint16_t i = 0; i < rx_len1; i++)
		{
			rx_buffer1[i] = 0; // 清接收缓存
		}
		rx_len1 = 0;			// 清除计数
		uart1recv_end_flag = 0; // 清除接收结束标志位
		HAL_UART_Receive_DMA(&huart1, (uint8_t *)rx_buffer1, buffsize1);
	}

	/* 串口3 */
	if (uart3recv_end_flag == 1)
	{
//    HAL_UART_Transmit(&huart3,rx_buffer3,rx_len3,0xff);
		uartData_depart(rx_buffer3, rx_len3);
		for (uint16_t i = 0; i < rx_len3; i++)
		{
			rx_buffer3[i] = 0; // 清接收缓存
		}
		rx_len3 = 0;			// 清除计数
		uart3recv_end_flag = 0; // 清除接收结束标志位
		HAL_UART_Receive_DMA(&huart3, (uint8_t *)rx_buffer3, buffsize3);
	}

}

u8 buffer_test[100] = {0};
void uartData_depart(u8 *buffer, u16 datalenth)
{
	u16 len;
	u16 cmdId;
	u8 sum;
	if (buffer[0] == 0x42 ) // 验证上位机帧头
//	if ((buffer[0] == 0x50 & buffer[1] == 0x43)|(buffer[0] == 0x43 & buffer[1] == 0x4C)|(buffer[0] == 0x44 & buffer[1] == 0x01)|(buffer[0] == 0x48 & buffer[1] == 0x44)) // 验证上位机帧头
	{
		//	debug("帧头;");
		len = buffer[2] << 8 | buffer[3];
		if (len < datalenth)
		{
			uartData_depart(buffer, len);
			uartData_depart(&buffer[len - 1], datalenth - len);
		}
		else if (len == datalenth) // 验证数据长度
		{
			// debug("长度;");
			if (buffer[len - 1] == 0xda) // 验证帧尾
			{
				// debug("帧尾;");
				sum = SumCheck(buffer, len - 2);
				if (sum == buffer[len - 2]) // 验证校验和
				{
					//				debug("校验和为%bx;",sum);
					cmdId = buffer[4] << 8 | buffer[5];
					memset(cmdData, 0, 100);
					memcpy(cmdData, buffer + 6, len - 8);	 // 有效数据拷贝出来并处理
															 //		uart_send(&huart1, 0x4000, &buffer[4], len - 6); // 收到数据回复
					process_PCdata(cmdData, cmdId, len - 8); // 协议字节为8位，剩余字节为有效数据
				}
			}
		}
	}
	
}

void uart_send(UART_HandleTypeDef *huart, u16 cmdID, u8 *TXmessage, u8 len) // 串口数据打包
{

	u8 exLen = 8; // 上位机协议固定8个字节，有效数据len个字节
	u8 tmp_sum;

	memcpy(tempdata, buf_FrZboard_head, 2); // 打包帧头和指令
	tempdata[3] = len + exLen;
	tempdata[len + exLen - 1] = 0xda; // 打包数据长度和帧尾
	tempdata[4] = cmdID >> 8 & 0xff;
	tempdata[5] = cmdID & 0xff;
	memcpy(&tempdata[6], TXmessage, len); // 打包数据
	tmp_sum = SumCheck(tempdata, len + exLen - 2);
	tempdata[len + exLen - 2] = tmp_sum; // 打包校验和

	HAL_UART_Transmit(huart, tempdata, len + exLen, 0xff);

}


void NRF_send(u16 cmdID, u8 *TXmessage, u8 len) 
{

	u8 exLen = 8; // 上位机协议固定8个字节，有效数据len个字节
	u8 tmp_sum;

	memcpy(tempdata, buf_FrZboard_head, 2); // 打包帧头和指令
	tempdata[3] = len + exLen;
	tempdata[len + exLen - 1] = 0xda; // 打包数据长度和帧尾
	tempdata[4] = cmdID >> 8 & 0xff;
	tempdata[5] = cmdID & 0xff;
	memcpy(&tempdata[6], TXmessage, len); // 打包数据
	tmp_sum = SumCheck(tempdata, len + exLen - 2);
	tempdata[len + exLen - 2] = tmp_sum; // 打包校验和

	if (NRF24L01_TxPacket(tempdata) == TX_OK){}
}
/////////////////////////////////     printf函数重写     ///////////////////////////////////////////////////////////
int fputc(int ch, FILE *f)
{
    HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xffff);
    return ch;
}

int fgetc(FILE *f)
{
    uint8_t ch = 0;
    HAL_UART_Receive(&huart1, &ch, 1, 0xffff);
    return ch;
}
